<template>
  <div class="tool-container">
    <el-form :inline="true" ref="form" :model="searchform" label-width="80px">
      <el-form-item>
        <el-input placeholder="请输入关键字" size="mini"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini">查询</el-button>
        <el-button type="primary" size="mini" @click="handleImport">导入</el-button>
      </el-form-item>
    </el-form>

    <div class="table">
      <el-table height="100%" :data="tableData" border>
        <el-table-column prop="tableName" label="业务表" align="center"></el-table-column>
        <el-table-column prop="tableComment" label="描述" align="center"></el-table-column>
        <el-table-column prop="tableEngine" label="数据库引擎" align="center"></el-table-column>
        <!-- <el-table-column prop="createTime" label="创建时间"align="center"></el-table-column> -->
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleUpdate(scope.$index,scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
            <el-button type="text" size="mini" @click="handleDownload(scope.$index,scope.row)">下载</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchform.pageNum"
      :page-sizes="[10, 15, 20]"
      :page-size="searchform.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { selectTableFromSystem, deleteTableFromSystem } from "@/api/gen";
export default {
  data() {
    return {
      tableData: [],
      searchform: {
        pageNum: 1,
        pageSize: 10,
        key: ""
      },
      total: 0
    };
  },
  created() {
    this.selectTableFromSystem();
  },
  methods: {
    async selectTableFromSystem() {
      const { data } = await selectTableFromSystem(this.searchform);
      this.tableData = data.data;
      this.searchform.pageNum = data.pageNum;
      this.searchform.pageSize = data.pageSize;
      this.total = data.total;
    },
    handleSizeChange(val) {
      this.searchform.pageSize = val;
      this.selectTableFromSystem(this.searchform);
    },
    handleCurrentChange(val) {
      this.searchform.pageNum = val;
      this.selectTableFromSystem(this.searchform);
    },
    handleImport() {
      this.$router.push("/systemtool/gen/save/import");
    },
    handleUpdate(index, row) {
      this.$router.push(`/systemtool/gen/save/${row.id}`);
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await deleteTableFromSystem([row.id]);
          if (index === 0) {
            this.searchform.pageNum--;
          }
          this.selectTableFromSystem();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleDownload(index, row) {
      console.log(row);
    }
  }
};
</script>

<style lang="scss" scoped>
.tool {
  &-container {
    background-color: #fff;
    height: 100%;
    margin: 30px;
    padding: 10px 0px 40px 10px;
  }
}
.table {
  height: 400px;
}
.el-pagination {
  margin-top: 40px;
  text-align: center;
}
</style>